<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>res_edit.html</title>
</head>
 <body>
 <script type="text/javascript">
 	var winAssignRes = new Windows("role_assign_resource");
 	var lastSelectId = null;
 	var isChanged = false;
 	var isInitBtn = true;
 	var isInitedRes = false;//表示资源是否已经在家并设置完毕
 	var roleResIds = '$!roleResBtns';
 	var objRoleResIds = new JSONObject();
 	$(function(){
 		initDataGrid();
 		initToolbar();
 		//初始化用户的权限
 		var oRoleResIds = JSONObject.parseJSON(roleResIds);
 		if(oRoleResIds.length>0){
 			$.each(oRoleResIds,function(i,v){
 				objRoleResIds.put(v.resId, v.buttonIds);
 			});
 		}
 	});
 	function initDataGrid(){
 		winAssignRes.find("#tgRoleResourceList").treegrid({
 				title:'资源列表',
 				iconCls:'icon-res',
				autoRowHeight: false,
				url:'$!basePath/admin/role/roleResource/$!role.id',
				border:false,
				fit:true,
				idField:'id',
				nowrap:false,
				treeField:'name',
				animate:true,
				singleSelect:false,
				checkOnSelect:false,
				selectOnCheck:false,
				frozenColumns:[[
	                {field:'id',width:40,checkbox:true}
				]],
				columns:[[			
					{field:'name',title:'资源名称',width:120,sortable:true
						,formatter:function(value,row,index){
							return highShowSearch(winAssignRes.find("#tgRoleResourceList"),"name",value);
						}
					},
					{field:'buttons',title:'按钮权限',width:300,formatter:function(value,row,index){
							var val = "";
							for(var i=0;i<value.length;i++){
								var btn = value[i];
								val += "<span class=\""+btn.icon+"\" style=\"padding-left:20px;padding-top:2px;\">"+btn.name+"</span>&nbsp;";
							}
							return val;
					}}
				]],
				rownumbers:true,
				onLoadSuccess:function(){
					var oRoleResIds = JSONObject.parseJSON(roleResIds);
			 		if(oRoleResIds.length>0){
			 			$.each(oRoleResIds,function(i,v){
			 				winAssignRes.find("#tgRoleResourceList").treegrid("checkRow",v.resId);
			 			});
			 		}
			 		isInitedRes = true;
				},
				onSelect:function(row){
					if(lastSelectId){
						winAssignRes.find("#tgRoleResourceList").treegrid("unselectRow",lastSelectId);
					}
					lastSelectId = row.id;
					isInitBtn = true;//标识为初始化按钮
					winAssignRes.find("#dgResourceBtnList").datagrid("load",{"resId":row.id});
				},
				onClickRow:function(row){
					isChanged = true;
				},
				onCheck:function(row){
					if(isInitedRes){
						isChanged = true;
					}
				},
				onUnCheck:function(row){
					if(isInitedRes){
						isChanged = true;
					}
				}
		});
		winAssignRes.find("#dgResourceBtnList").datagrid({
 				title:'资源按钮',
 				iconCls:'icon-button-add',
 				autoRowHeight: false,
				url:'$!basePath/admin/res/resourceButtonsForRole',
				border:false,
				fit:true,
				idField:'id',
				animate:true,
				singleSelect:false,
				frozenColumns:[[
	                {field:'id',checkbox:true}
				]],
				columns:[[	
					{field:'icon',title:'图标',width:40,formatter:function(value,row,index){
						value = "<center><span style='padding-top:2px' class='"+value+"'\>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></center>"
						return value;
					}},		
					{field:'name',title:'按钮名称',width:110,sortable:true}
				]],
				rownumbers:true,
				onLoadSuccess:function(){
					if(lastSelectId){
						var btnIds = objRoleResIds.get(lastSelectId);
						winAssignRes.find("#dgResourceBtnList").datagrid("unselectAll");
						if(btnIds){
							$.each(btnIds,function(i,v){
								winAssignRes.find("#dgResourceBtnList").datagrid("selectRecord",v);
							});
						}
						isInitBtn = false;
					}
				},
				onSelectAll:function(rows){
					if(isInitBtn)
						return;
					doBtnSelectEvent();
				},
				onSelect:function(row){
					if(isInitBtn)
						return;
					doBtnSelectEvent();
				},
				onUnselect:function(row){
					if(isInitBtn)
						return;
					doBtnSelectEvent();
				}
 		});
 	}
 	//右边按钮选择列表变化时左边列表跟着变化
 	function doBtnSelectEvent(){
 		var selectRows = winAssignRes.find("#dgResourceBtnList").datagrid("getSelections");
		var btnArray = new Array();
		$.each(selectRows,function(i,v){
			btnArray.push(v.id);
		});
		objRoleResIds.put(lastSelectId, btnArray);
 		var buttons = new Array();
		//更新左边表格中的按钮显示
		$.each(selectRows,function(i,v){
			var btn = new JSONObject();
			btn.put("id", v.id);
			btn.put("name",v.name);
			btn.put("icon",v.icon);
			
			buttons.push(btn);
		});
		if(buttons.length>0){
			winAssignRes.find("#tgRoleResourceList").treegrid("update",{
				id:lastSelectId,
				row:{
					buttons:eval("(["+buttons.toString()+"])")
				}
			});
		}
 	}
 	function initToolbar(){
 		//重置
 		winAssignRes.find("#btnReset").click(function(){
 			winAssignRes.find("#tgRoleResourceList").treegrid("load","$!basePath/admin/res/resList");
 		});
 	}
 	//保存已分配的资源
 	function doSave(ifClose){
			var ids = getSelectResIds();
			ids = ids.replace("[", "").replace("]", "");
			var roleid = $!role.id;
			$.post("$!basePath/admin/role/doAssignResource",{roleid:roleid,res:ids},function(data){
				if(data.isSaved){
					if(ifClose){
						top.Notiy.success("分配资源成功!");
						winAssignRes.close();
					}else{
						isChanged = false;
						top.Notiy.success("保存成功!");
					}
				}else{
					top.Dialog.error(data.error);
				}
			},"json");
 	}
 	//获取选中的ID
 	function getSelectResIds(){
 		var checkboxs = winAssignRes.find("#tgRoleResourceLayout").find("input[name='id'][type='checkbox']");
 		var selectRes_Ids = new JSONObject();
 		for(var i=0;i<checkboxs.length;i++){
 			var ck = checkboxs[i];
 			if(ck.checked){
 				if(objRoleResIds.containsKey(ck.value)){
 					var btns = objRoleResIds.get(ck.value);
 					selectRes_Ids.put(ck.value,btns);
 				}else{
 					selectRes_Ids.put(ck.value,"");
 				}
 			}else{
 				if(objRoleResIds.containsKey(ck.value)){
 					objRoleResIds.remove(ck.value);
 				}
 			}
 		}
 		return "["+selectRes_Ids.toString()+"]";
 	}
 	function doSearch(v,n){
 		doTreeGridSearch("tgRoleResourceList","$!basePath/admin/res/resList",n,v);
 	}
	function a_res_ok(){
		if(isChanged){
			doSave(1);
 		}else{
 			winAssignRes.close();
 			if(!lastSelectId)
 				top.Notiy.info("您未做任何修改!");
 		}
  	}
  	function a_res_cancel(){
  		if(isChanged){
	  		top.Dialog.confirm("提示","您已做出修改，是否保存?",function(r){
		 			if(r){
						doSave(1);
					}else{
						winAssignRes.close();
					}
		 	});
	 	}else{
	 		winAssignRes.close();
	 	}
  	}
 </script>
 <div class="easyui-layout" data-options="fit:true">
 	<!-- 左边待选资源菜单 -->
	 <div data-options="region:'center',border:false" id="tgRoleResourceLayout" style="background:#fff;border-bottom:1px solid #ccc;">
	 	<table id="tgRoleResourceList"></table>
	 </div>
	 <!-- 右边待选资源按钮 -->
	 <div data-options="region:'east',border:false,split:true" style="width:220px;background:#fff;border-bottom:1px solid #ccc;">
	 	<table id="dgResourceBtnList"></table>
	 </div>
	 <!--Change Button Text-->
	 <div data-options="region:'south',border:false" style="text-align:center;padding:5px 0;">
		<a id="ok" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="a_res_ok()">确定</a>
		<a id="cancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="a_res_cancel()">取消</a>
	</div>
 </div>
  </body>
</html>